//账号管理模块-查信息有效性
var xhr='';
//错误的样式展示
function changeState_Wrong(input,msg,imgRight,imgWrong){
    imgWrong.style.display="inline";
    input.style.borderColor="red";
    msg.style.color="red";
    imgRight.style.display="none";
    msg.style.fontSize="14px";
}
//正确的样式展示
function changeState_Right(input,imgRight,imgWrong){
    imgRight.style.display="inline";
    input.style.borderColor="green";
    imgWrong.style.display="none";
}

//下面是其它的、只用js就可以完成的操作
//1.密码
var elPassword=document.getElementById('password');
var elPasswordMsg=document.getElementById('passwordAvailability');
function checkPassword(password){
    console.log(password);
    if(password===''||password===null){
        elPasswordMsg.textContent='密码不能为空';
        changeState_Wrong(document.getElementById("password"),document.getElementById("passwordAvailability"),document.getElementById("right_password"),document.getElementById("wrong_password"));

    }else{
        elPasswordMsg.textContent='';
        changeState_Right(document.getElementById("password"),document.getElementById("right_password"),document.getElementById("wrong_password"));
    }
}
elPassword.addEventListener('blur', function() {
    checkPassword(elPassword.value);
});
//预期设置密码的强弱

//2.重复密码
var elRepeatedPassword=document.getElementById('repeatedPassword');
var elRepeatedPwdMsg=document.getElementById('repeatedPasswordAvailability');
function checkRepeatedPwd(password,repeatedPassword){
    if(password===repeatedPassword){
        elRepeatedPwdMsg.textContent='';
        changeState_Right(document.getElementById("repeatedPassword"),document.getElementById("right_repeatedPassword"),document.getElementById("wrong_repeatedPassword"));
    }else{
        elRepeatedPwdMsg.textContent='两次密码不一致';
        changeState_Wrong(document.getElementById("repeatedPassword"),document.getElementById("repeatedPasswordAvailability"),document.getElementById("right_repeatedPassword"),document.getElementById("wrong_repeatedPassword"));
    }
}
elRepeatedPassword.addEventListener('blur', function() {
    checkRepeatedPwd(elPassword.value,elRepeatedPassword.value);
});

//3.邮箱
var elEmail=document.getElementById('email');
var elEmailMsg=document.getElementById('emailAvailability');
function checkEmail(email){
    var regex = /^[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/;
    if(email===''||email===null){
        elEmailMsg.textContent='邮箱不能为空';
        changeState_Wrong(document.getElementById("email"),document.getElementById("emailAvailability"),document.getElementById("right_email"),document.getElementById("wrong_email"));
        return;
    }
    elEmailMsg.textContent='';
    if(regex.test(email)){
        //有效邮箱格式
        elEmailMsg.textContent='';
        changeState_Right(document.getElementById("email"),document.getElementById("right_email"),document.getElementById("wrong_email"));
    }else{
        //无效邮箱格式
        elEmailMsg.textContent='邮箱格式无效';
        changeState_Wrong(document.getElementById("email"),document.getElementById("emailAvailability"),document.getElementById("right_email"),document.getElementById("wrong_email"));
    }

}
elEmail.addEventListener('blur', function() {
    checkEmail(elEmail.value);
});
//4.电话
var elPhone=document.getElementById('phone');
var elPhoneMsg=document.getElementById('phoneAvailability');
function checkPhone(phone){
    var regex = /^\d{10}$/;
    if(phone===''||phone===null){
        elPhoneMsg.textContent='电话不能为空';
        changeState_Wrong(document.getElementById("phone"),document.getElementById("phoneAvailability"),document.getElementById("right_phone"),document.getElementById("wrong_phone"));
        return;
    }
    elPhoneMsg.textContent='';
    if(regex.test(phone)){
        //有效电话格式
        elPhoneMsg.textContent='';//十个数字
        changeState_Right(document.getElementById("phone"),document.getElementById("right_phone"),document.getElementById("wrong_phone"));
    }else{
        //无效电话格式
        elPhoneMsg.textContent='电话格式无效';
        changeState_Wrong(document.getElementById("phone"),document.getElementById("phoneAvailability"),document.getElementById("right_phone"),document.getElementById("wrong_phone"));
    }
}
elPhone.addEventListener('blur', function() {
    checkPhone(elPhone.value);
});
//5.地址、城市、state、国家 不能为空
var elAddress1=document.getElementById('address1');
var elAddress1Msg=document.getElementById('address1Availability');
var elAddress2=document.getElementById('address2');
var elAddress2Msg=document.getElementById('address2Availability');
var elCity=document.getElementById('city');
var elCityMsg=document.getElementById('cityAvailability');
var elState=document.getElementById('state');
var elStateMsg=document.getElementById('stateAvailability');
var elCountry=document.getElementById('country');
var elCountryMsg=document.getElementById('countryAvailability');
//检查非空的函数
function checkNotEmpty(element, messageElement, errorMessage,right_element,wrong_element) {
    var value = element.value.trim(); // 去除字符串两端的空白字符

    if (value === '') {
        messageElement.textContent = errorMessage;
        changeState_Wrong(element,messageElement,right_element,wrong_element);
        return false; // 返回 false 表示非空检查失败
    } else {
        messageElement.textContent = ''; // 清空错误消息
        changeState_Right(element,right_element,wrong_element);
        return true; // 返回 true 表示非空检查成功
    }
}
function handleInputChange() {
// 根据事件目标调用相应的非空检查函数和显示信息
    if (event.target === elAddress1) {
        checkNotEmpty(elAddress1, elAddress1Msg, 'address1不能为空',document.getElementById("right_address1"),document.getElementById("wrong_address1"));
    } else if (event.target === elAddress2) {
        checkNotEmpty(elAddress2, elAddress2Msg, 'address2不能为空',document.getElementById("right_address2"),document.getElementById("wrong_address2"));
    } else if (event.target === elCity) {
        checkNotEmpty(elCity, elCityMsg, 'city不能为空',document.getElementById("right_city"),document.getElementById("wrong_city"));
    } else if (event.target === elState) {
        checkNotEmpty(elState, elStateMsg, 'State不能为空',document.getElementById("right_state"),document.getElementById("wrong_state"));
    } else if (event.target === elCountry) {
        checkNotEmpty(elCountry, elCountryMsg, 'country不能为空',document.getElementById("right_country"),document.getElementById("wrong_country"));
    }
}

// 为每个输入元素添加输入事件监听器
elAddress1.addEventListener('blur', handleInputChange);
elAddress2.addEventListener('blur', handleInputChange);
elCity.addEventListener('blur', handleInputChange);
elState.addEventListener('blur', handleInputChange);
elCountry.addEventListener('blur', handleInputChange);
//6.邮编判断
var elZip=document.getElementById('zip');
var elZipMsg=document.getElementById('zipAvailability');
function checkZip(zip){
    var regex = /^\d{5}$/;//五位数字
    if(zip===''||zip===null){
        elZipMsg.textContent='邮编不能为空';
        changeState_Wrong(document.getElementById("zip"),document.getElementById("zipAvailability"),document.getElementById("right_zip"),document.getElementById("wrong_zip"));
        return;
    }
    elZipMsg.textContent='';
    if(regex.test(zip)){
        //有效邮编格式
        elZipMsg.textContent='';
        changeState_Right(document.getElementById("zip"),document.getElementById("right_zip"),document.getElementById("wrong_zip"));
    }else{
        //无效邮编格式
        elZipMsg.textContent='邮编格式无效';
        changeState_Wrong(document.getElementById("zip"),document.getElementById("zipAvailability"),document.getElementById("right_zip"),document.getElementById("wrong_zip"));
    }
}
elZip.addEventListener('blur', function() {
    checkZip(elZip.value);
});

